package com.payment.notify.receive.service.handler.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.payment.common.dto.notify.NotifyDto;
import com.payment.common.exceptions.ServiceException;
import com.payment.notify.receive.annotation.RequestHandler;
import com.payment.notify.receive.service.NotifyReceiveStrategy;
import com.payment.notify.receive.service.handler.NotifyReceiveHandler;

/**
 * 类名：MessageDefaultHandler.java<br>
 * 描述：消息模块业务处理,默认使用反射获取方法,
 * 如果无法满足特定需求,请新建一个类实现MessageReqHandler,重写业务<br>
 * 创建者：XINJF<br>
 * 创建日期：2017年11月30日 下午1:36:30<br>
 * 版本：1.0<br>
 * 修改者：<br>
 * 修改日期：<br>
 */
public class NotifyReceiveDefaultHandler implements NotifyReceiveHandler {

	@Override
	public NotifyReceiveStrategy doRequest(NotifyDto parameterDto) throws ServiceException {
		
		try {
			if(parameterDto.getClass().isAnnotationPresent(RequestHandler.class)) {
				RequestHandler requestHandler = parameterDto.getClass().getAnnotation(RequestHandler.class);
				final Class<?> classzz = requestHandler.classzz();
				final String methodName = requestHandler.methodName();
				final Object object = classzz.newInstance();
//				final Map<String,Object> parameters = MessageReqUtil.getReqParam(parameterDto);
				final Method method = object.getClass().getMethod(methodName, NotifyDto.class);
				return (NotifyReceiveStrategy) method.invoke(object, parameterDto);
			}
		} catch (InstantiationException 
					| IllegalAccessException 
					| IllegalArgumentException 
					| InvocationTargetException 
					| NoSuchMethodException 
					| SecurityException e) {
			e.printStackTrace();
		}
		return null;
	}

}
